package com.example.badyloans.servlet;

import com.example.badyloans.common.ReturnResult;
import com.example.badyloans.dao.TBankCardMapper;
import com.example.badyloans.dao.TUserWalletMapper;
import com.example.badyloans.pojo.TAccountFlow;
import com.example.badyloans.pojo.TBankCard;
import com.example.badyloans.pojo.TUserWallet;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.example.badyloans.pojo.TWithdraw;
import com.example.badyloans.dao.TWithdrawMapper;
import com.example.badyloans.servlet.TWithdrawService;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;

@Service
public class TWithdrawServiceImpl implements TWithdrawService{
    @Resource
    private TBankCardMapper tBankCardMapper;
    @Resource
    private TWithdrawMapper tWithdrawMapper;
    @Resource
    private TUserWalletMapper tUserWalletMapper;
    @Resource
    private TUserWalletService tUserWalletService;
    @Resource
    private TAccountFlowService tAccountFlowService;
    @Override
    public int deleteByPrimaryKey(String id) {
        return tWithdrawMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ReturnResult insertSelective(TWithdraw tWithdraw) {
        tWithdraw.setId(UUID.randomUUID().toString().replace("-","".toLowerCase()));
        tWithdraw.setCreateTime(LocalDateTime.now());
        //根据映射的值
        //对提现表进行新增
        int count=tWithdrawMapper.insertSelective(tWithdraw);
        if(count>0){
            //新增完成之后   进行用户资金查询
            TUserWallet tUserWallet= tUserWalletMapper.selectByPrimaryKey(tWithdraw.getUserId());
            //判断是否为空
            if(tUserWallet!=null){
                //对用户资金表进行修改
                tUserWallet.setAvailableAmount(tUserWallet.getAvailableAmount()-tWithdraw.getAmount()-tWithdraw.getFee());
                int count1=tUserWalletMapper.updateByPrimaryKeySelective(tUserWallet);
                if(count1>0){
                    //对银行表进行修改记录
                    TBankCard tBankCard = tBankCardMapper.selectByPrimaryKey(tWithdraw.getUserId());
                    tBankCard.setBalance(tBankCard.getBalance()+tWithdraw.getAmount()+tWithdraw.getFee());
                    int count2=tBankCardMapper.updateByPrimaryKeySelective(tBankCard);
                    if(count2>0){
                        TAccountFlow tAccountFlow = new TAccountFlow();
                        Long tradeNo1= System.currentTimeMillis()+Math.abs(new Random().nextInt());
                        tAccountFlow.setId(tradeNo1);
                        tAccountFlow.setAccountId(tUserWallet.getAccountId());
                        tAccountFlow.setAmount(tWithdraw.getAmount());
                        tAccountFlow.setFlowType(60);
                        tAccountFlow.setAvailableAmount(tUserWallet.getAvailableAmount());
                        tAccountFlow.setFreezeAmount(tUserWallet.getFreezeAmount());
                        tAccountFlow.setRemark("提现成功，提现金额："+tWithdraw.getAmount()*0.01+"元");
                        tAccountFlow.setCreateTime(LocalDateTime.now());
                        int count3=tAccountFlowService.insertSelective(tAccountFlow);
                        if(count3>0){
                            return ReturnResult.succes();
                        }else{
                            return ReturnResult.fail("流水记录失败");
                        }
                    }else{
                        return ReturnResult.fail("更新银行卡余额失败");
                    }
                }else{
                    return ReturnResult.fail("更新用户资金失败");
                }
            }else{
                return ReturnResult.fail("数据查询失败");
            }
        }else {
            return ReturnResult.fail("提现失败");
        }
    }

    @Override
    public TWithdraw selectByPrimaryKey(String id) {
        return tWithdrawMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TWithdraw record) {
        return tWithdrawMapper.updateByPrimaryKeySelective(record);
    }

}
